Xbasic

Office::ExcelDocumentSheetToJson Method

Syntax

.SheetToJson as C (FileName as C [, SheetName as C [, ReturnAsObjects as L = .t.]])

Arguments

FileNameCharacter

The Excel file to read. File must end in .xlsx extension.

SheetNameCharacter

Default = the first sheet in the file. The name of the sheet to read.

ReturnAsObjectsLogical

Default = .t.. If .t., each row in the sheet is structured as name-value pairs where the name is assumed to be the value in the first row of the column. If .f., the JSON is returned as an array of arrays where each row in the array corresponds to the row in the sheet. See examples below.

Returns

resultCharacter

Returns the data in the Excel file as a JSON string.

Description

Get a specific sheet and cell values as JSON.

Example

dim doc as Office::ExcelDocument
dim filename as c = "C:\Users\JaneDoe\Documents\excelDoc.xlsx"

json1 = doc.sheetToJson(filename,"Sheet1",.t.)
? json1
= [
{"Firstname" : "John" , "Lastname" : "Smith" , "City" : "Boston" , "State" : "MA"} , 
{"Firstname" : "Henry" , "Lastname" : "Rhodes" , "City" : "New York" , "State" : "NY"} , 
{"Firstname" : "Allison" , "Lastname" : "Berman" , "City" : "Los Angeles" , "State" : "CA"} , 
{"Firstname" : "Amanda" , "Lastname" : "Higgins" , "City" : "Chicago" , "State" : "IL"} , 
{"Firstname" : "Nancy" , "Lastname" : "Clark" , "City" : "Boston" , "State" : "MA"} , 
{"Firstname" : "Cecelia" , "Lastname" : "Dawkins" , "City" : "Boulder" , "State" : "CO"} , 
{"Firstname" : "Kathy" , "Lastname" : "Morton" , "City" : "New York" , "State" : "NY"}]


json2 = doc.sheetToJson(filename,"Sheet1",.f.)
? json2
= [
["Firstname" , "Lastname" , "City" , "State"] , 
["John" , "Smith" , "Boston" , "MA"] , 
["Henry" , "Rhodes" , "New York" , "NY"] , 
["Allison" , "Berman" , "Los Angeles" , "CA"] , 
["Amanda" , "Higgins" , "Chicago" , "IL"] , 
["Nancy" , "Clark" , "Boston" , "MA"] , 
["Cecelia" , "Dawkins" , "Boulder" , "CO"] , 
["Kathy" , "Morton" , "New York" , "NY"]]

See Also